//JavaScript Framework 2.0 Code
try {
	Type.registerNamespace('com.yonyou.ncmm_pac_pda.MaterialOutQueryConController');
	com.yonyou.ncmm_pac_pda.MaterialOutQueryConController = function() {
		com.yonyou.ncmm_pac_pda.MaterialOutQueryConController.initializeBase(this);
		this.initialize();
	}

	function com$yonyou$ncmm_pac_pda$MaterialOutQueryConController$initialize() {

	}

	function com$yonyou$ncmm_pac_pda$MaterialOutQueryConController$evaljs(js) {
		eval(js)
	}

	//右上角公共菜单
	function loadmenu(sender, args) {
		NCMMPubMenu.openDropDownList("button2");
	}

	//从缓存中取上一次查询时保存的查询条件
	function defaultConditionLoad(sender, args) {
		var defaultcondition = $cache.read("defaultcondition") ? $cache.read("defaultcondition") : "";

		if(defaultcondition != "") {
			defaultcondition = $stringToJSON(defaultcondition);
			var pk_material = defaultcondition.material ? (defaultcondition.material)["pk_material"] : "";
			if(pk_material != "") {
				var queryvos_material = NCMMDBUtil.getVOsByPK(MaterialVO, MaterialVO.PKMATERIAL.name, pk_material);
				var queryvo_material = null;
				if(queryvos_material) {
					queryvo_material = queryvos_material[0];
				}
				if(queryvo_material) {
					//					defaultcondition["material"] = queryvo_material;
					$ctx.put("material", NCMMPubUtil.htmlspecialjson2UI(queryvo_material));
					$ctx.dataBind();
				}
			}

			var cdptid = defaultcondition.dept ? (defaultcondition.dept)["pk_dept"] : "";
			if(cdptid != "") {
				var queryvos_dept = NCMMDBUtil.getVOsByPK(DeptVO, DeptVO.PKDEPT.name, cdptid);
				var queryvo_dept = null;
				if(queryvos_dept) {
					queryvo_dept = queryvos_dept[0];
				}
				if(queryvo_dept) {
					//					defaultcondition["dept"] = queryvo_dept;
					$ctx.put("dept", NCMMPubUtil.htmlspecialjson2UI(queryvo_dept));
					$ctx.dataBind();
				}
			}
			//			$ctx.push(defaultcondition);
		} else {
			var billdate = {
				"startdate": (NCMMDateUtil.getDayDate())["beginDate"],
				"enddate": (NCMMDateUtil.getDayDate())["endDate"]
			};
			$ctx.put("billdate", billdate);
			$ctx.dataBind();
		}

	}

	//清空所有条件
	function clearDateCondition(sender, args) {
		$ctx.push({});
	}

	//查询按钮单击事件，组装查询条件
	function com$yonyou$ncmm_pac_pda$MaterialOutQueryConController$button1_onclick(sender, args) {
		var condition = {};
		var cachecondition = {};
		$ctx.dataCollect();
		//		$alert("material = " + $ctx.getString("material"));
		var material = $ctx.getString("material");
		//加入物料编码
		if(material != "") {
			material = $stringToJSON(material);
			material = NCMMPubUtil.htmlspecialjson2UI(material);
			if(material[MaterialVO.PKMATERIAL.name] != null) {
				condition["pk_material"] = "='" + material[MaterialVO.PKMATERIAL.name] + "'";
				cachecondition["material"] = material;
			}
		}

		//		$alert("dept = " + $ctx.getString("dept"));
		//加入领料部门主键
		var dept = $ctx.getString("dept");
		if(dept != "") {
			dept = $stringToJSON(dept);
			dept = NCMMPubUtil.htmlspecialjson2UI(dept);
			if(dept[DeptVO.PKDEPT.name] != null) {
				condition["cdptid"] = "='" + dept[DeptVO.PKDEPT.name] + "'";
				cachecondition["dept"] = dept;
			}
		}

		//		$alert("vbillcode = " + $ctx.getString("vbillcode"));
		//加入单据号
		var vbillcode = $ctx.getString("vbillcode");
		//处理反斜杠
		vbillcode = NCMMPubUtil.htmlspecialchars2UI(vbillcode);
		if(vbillcode != "") {
			cachecondition["vbillcode"] = vbillcode;
			//处理单引号
			vbillcode = NCMMMOUtil.htmlsqmchars2UI(vbillcode);
			condition[MaterialOutVO.VBILLCODE.name] = "='" + vbillcode + "'";
		}

		//		$alert("vbillcode = " + $ctx.getString("vbillcode"));
		//加入批次号
		var vbatchcode = $ctx.getString("vbatchcode");
		vbatchcode = NCMMPubUtil.htmlspecialchars2UI(vbatchcode);
		if(vbatchcode != "") {
		    cachecondition["vbatchcode"] = vbatchcode;
		    //处理单引号
			vbatchcode = NCMMMOUtil.htmlsqmchars2UI(vbatchcode);
			condition[MaterialOutVO.VBATCHCODE.name] = "='" + vbatchcode + "'";
		}

		//		$alert("billdate = " + $ctx.getString("billdate"));
		//加入日期区间
		var billdate = $ctx.getString("billdate");
		if(billdate != "") {
			billdate = $stringToJSON(billdate);
			if(!((billdate.startdate == null || billdate.startdate == "") && (billdate.enddate == null || billdate.enddate == ""))) {
				if((billdate.startdate == null || billdate.startdate == "")) {
					$alert("请选择起始日期！");
					return;
				}
				if((billdate.enddate == null || billdate.enddate == "")) {
					$alert("请选择结束日期！");
					return;
				}
				billdate.startdate = billdate.startdate + " 00:00:00";
				billdate.enddate = billdate.enddate + " 23:23:59";
				condition[MaterialOutVO.DBILLDATE.name] = "between '" + billdate["startdate"] + "' and '" + billdate["enddate"] + "'";
				cachecondition["billdate"] = billdate;
			}
		}

		//		$alert("condition = " + $jsonToString(condition));
		$cache.write("defaultcondition", $jsonToString(cachecondition));
		//返回组装的条件
		$view.closeWithCallBack({
			"data": condition
		});
	}

	//back按钮点击事件
	function com$yonyou$ncmm_pac_pda$MaterialOutQueryConController$button0_onclick(sender, args) {
		// 返回就关掉页面
		$view.close();

	}

	function mycallback() {
		//get和post的CallBack中获取返回结果都从result中获取
		var result = $ctx.param("result");
		if(result == null || result == "") {
			$alert("没有找到匹配的生产订单,请重新输入");
			return;
		}
		//将字符串转换成JSON对象
		result = $stringToJSON(result);
		if(result != null) {
			// 返回的结构里是result里嵌入result，所以要想取到json数组要嵌套取值
			result = result.result;
			// $alert(result);
		}
		$view.open({
			"viewid": "com.yonyou.ncmm_pac_pda.FPInListPage",
			"isKeep": "true",
			"result": result
		});
	}

	//参照物料
	function refMaterial(sender, args) {
		//打开指定参照界面
		openRefPage("PDAMATERIAL", "material");
	}
	//参照部门
	function refDept(sender, args) {
		openRefPage("PDADEPT", "dept");
	}

	// 打开参照页面
	function openRefPage(ncbillcode, ctxkey) {
		var jsonparam = {};
		getRefParam(ncbillcode, function(data) {
			jsonparam = data;

			if(jsonparam == null) {
				//			$toast("jsonparam is null!");
				return;
			}
			$view.open({
				viewid: "com.yonyou.ncmm_pac_pda.RefPage", //目标页面
				isKeep: "true", //打开新页面的同时是否保留当前页面，true为保留，false为不保留
				data: jsonparam,
				callback: function() {
					var refdata = $param.getJSONObject("data");
					refdata = NCMMPubUtil.htmlspecialjson2UI(refdata);
					var queryvo = null;
					if(ncbillcode == "PDAMATERIAL") {
						var queryvos = NCMMDBUtil.getVOsByPK(MaterialVO, MaterialVO.PKMATERIAL.name, refdata[MaterialVO.PKMATERIAL.name]);
						if(queryvos) {
							queryvo = queryvos[0];
						}
					} else if(ncbillcode == "PDADEPT") {
						var queryvos = NCMMDBUtil.getVOsByPK(DeptVO, DeptVO.PKDEPT.name, refdata[DeptVO.PKDEPT.name]);
						if(queryvos) {
							queryvo = queryvos[0];
						}
					}
					//					$alert("queryvo="+$jsonToString(queryvo));
					if(queryvo) {
						$ctx.put(ctxkey, NCMMPubUtil.htmlspecialjson2UI(queryvo));
						$ctx.dataBind();
					}
				}
			});

		});

	}

	//获取参照vo的vo信息
	//reftype： 外系统调用标识
	function getRefParam(reftype, callback) {
		var condition = {
			"ncbillcode": reftype
		};
		NCMMPubUtil.queryVOByConditions(AppmappingheadVO, condition, function(data) {
			var jsondata = {
				bdlistdata: eval(data)
			};
			var reftitle = (jsondata.bdlistdata)[0]["appbillname"];
			if((jsondata.bdlistdata)[0]["downloadnum"] == "" || (jsondata.bdlistdata)[0]["downloadnum"] == "0") {
				$alert("【" + reftitle + "】没有可参照的数据");
				if(callback != null && callback != undefined && typeof(callback) == 'function') {
					callback(null);
					return;
				}
			}
			var reftablename = (jsondata.bdlistdata)[0]["apptablename"]; //本地表名
			var jsonparam = {
				reftype: reftype, //外系统调用标识
				reftitle: reftitle, //元数据显示名称
				reftablename: reftablename //本地表名
			};
			if(callback != null && callback != undefined && typeof(callback) == 'function') {
				callback(jsonparam);
			}
		});
	}

	com.yonyou.ncmm_pac_pda.MaterialOutQueryConController.prototype = {
		button1_onclick: com$yonyou$ncmm_pac_pda$MaterialOutQueryConController$button1_onclick,
		button0_onclick: com$yonyou$ncmm_pac_pda$MaterialOutQueryConController$button0_onclick,
		initialize: com$yonyou$ncmm_pac_pda$MaterialOutQueryConController$initialize,
		evaljs: com$yonyou$ncmm_pac_pda$MaterialOutQueryConController$evaljs
	};
	com.yonyou.ncmm_pac_pda.MaterialOutQueryConController.registerClass('com.yonyou.ncmm_pac_pda.MaterialOutQueryConController', UMP.UI.Mvc.Controller);
} catch(e) {
	$e(e);
}